package servlet.hou;

import bean.Build;
import bean.Result;
import bean.Student;
import bean.Teacher;
import com.fasterxml.jackson.databind.ObjectMapper;
import constants.Constant;
import dao.UserDao;
import dao.UserDaoImpl;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;



//管理员获取所有学生信息
@WebServlet("/hou/getStu")
public class HouGetStuServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_token = request.getHeader("user_token_header");
       Integer buildId = JwtUtils.getHouBuildIdFromToken(user_token, Constant.USER_CLAIM_HOU_BUILDID);
        Integer currentPage= Integer.valueOf(request.getParameter("currentPage"));
        System.out.println(buildId);
        System.out.println(currentPage);

        List<Student> student = new UserDaoImpl().findStuPageByBuildId(buildId,currentPage);

        Result result = new Result();
        if(student!=null){
            result.success(student);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }


    }
}
